﻿using System;
using Microsoft.Xna.Framework;

namespace Firefly.Framework.ContentManagement
{
    public class ContentService : IDisposableEx
    {
        public ContentTracker ContentManager { get; private set; }

        public void Initialize(Game game)
        {
            ContentManager = new ContentTracker(game.Services);
        }

        public T Load<T>(string assetName)
        {
            return ContentManager.Load<T>(assetName);
        }

        public T Load<T>(string assetName, AssetTracker tracker)
        {
            return ContentManager.Load<T>(assetName, tracker);
        }

        public AssetTracker LoadAsync<T>(string assetName, AssetLoaded itemLoadedMethod)
        {
            return LoadAsync<T>(assetName, itemLoadedMethod);
        }

        public void Unload()
        {
            ContentManager.Unload();
        }

        public void Unload(string assetName)
        {
            Unload(assetName, true);
        }

        public void Unload(string assetName, bool releaseChildren)
        {
            ContentManager.Unload(assetName, releaseChildren);
        }

        public void Release(string assetName)
        {
            ContentManager.Release(assetName);
        }

		#region Threadsafe Singleton
		
        public static ContentService Instance
        {
            get { return Nested._instance; }
        }

        private class Nested
        {
			internal static readonly ContentService _instance = new ContentService();
        
            static Nested() { }
        }

        private ContentService() 
        { 
        }
        
        #endregion

        #region IDisposableEx Members

        public bool Disposed { get; private set; }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        public void Dispose(bool disposing)
        {
            if (!Disposed)
            {
                if (disposing)
                {
                    ContentManager.Dispose();
                }

                Disposed = true;
            }
        }

        ~ContentService()
        {
            Dispose(true);
        }

        #endregion
    }
}
